/* 
   KARAN GUPTA - 2010037
   LIST IMPLEMENTATION USING ARRAYS
*/

# include <stdio.h>
# include <stdlib.h>

int N = 0;
int *A = NULL;

void append (int num)
{
  A = (int *)realloc(A,(sizeof(int)*(N+1)));
  A[N] = num;
  N++;
}

void extend ()
{
  int size,i;
  printf("Enter the size of the new list to be extended: ");
  scanf("%d",&size);
  int B[size];
  printf("Enter the list to be extended in the format a,b,c: ");
  for ( i=0;i<size;i++ )
    scanf("%d,",&B[i]);
  for ( i=0;i<size;i++ )
    append(B[i]);
  
}

void insert (int num,int loc)
{
  int i,temp;
  temp = A[loc-1];
  A[loc-1] = num;
  A = (int *)realloc(A,(sizeof(int)*(N+1)));
  N++;
  for (i = N; i>loc; i--)
     A[i] = A[i-1]; 
  A[loc] = temp;
}

int delete (int num)
{
   int i=0,j,temp;
   while (A[i] != num)
   {
      i++;
   }
   if (i != N)
   {
     for (j=i;j<N;j++)
         {
           temp = A[j]; 
           A[j] = A[j+1];
           A[j+1] = temp;
         }
     A= (int *)realloc(A,(sizeof(int)*(N-1)));
     N--;
   }
   else
   { 
     printf ("No such element exists!\n");
   }
}
int pop (int pos)
{
  int poppednum,j,temp;
  poppednum = A[pos-1];
     for (j=pos-1;j<N;j++)
         {
           temp = A[j]; 
           A[j] = A[j+1];
           A[j+1] = temp;
         }
     A= (int *)realloc(A,(sizeof(int)*(N-1)));
     N--;  
  return poppednum;
}

indexofnum (int num)
{
  int place = 0;
  while ( A[place] != num )
  {
    place++;
  }
  if ( place >= 1 && place<=N )
    printf("Element %d, first found at position %d\n", num, place+1);
  else
    printf("No such element!\n");    

}

count (int num)
{
  int i,counter=0;
  for ( i=0; i<N; i++)
  {
     if (A[i] == num)
       counter++;
  }
  printf("%d appears %d number of times\n",num,counter);
}

sort ()
{
  int i,j,temp;
  for ( i = 0 ; i < N-1 ; i++ )
  {
    for ( j = i + 1 ; j < N; j++ )
    {
       if (A[i] > A[j])
       {
	temp = A[i];
	A[i] = A[j];
	A[j] = temp ;
       }
    }
  } 
}

reverse ()
{
  int i,j,temp;
  for ( i=0,j=N-1;i<=(N/2);i++,j--)
  {
     temp = A[i];
     A[i] = A[j];
     A[j] = temp;
  }
}

display ()
{
  printf ("[ ");
  int i;
  for (i=0;i<N;i++)
    printf ("%d,",A[i]);
  printf (" ]\n");
}


void main()
{
  system("clear");
  A = (int*) malloc(sizeof(int) * N);
  int i;
  int choice;
  while (1)
  {  
    printf("\n1.  APPEND - Add an item to the end of the list\n");
    printf("2.  EXTEND - Extend the list by appending all the items in the given list\n");
    printf("3.  INSERT - Insert an item at a given position\n");
    printf("4.  REMOVE - Remove the first item from the list whose value is x\n");
    printf("5.  POP    - Remove the item at the given position in the list, and return it\n");
    printf("6.  INDEX  - Return the index in the list of the first item whose value is x\n");
    printf("7.  COUNT  - Return the number of times x appears in the list\n");
    printf("8.  SORT   - Sort the items in the list\n");
    printf("9.  REVERSE- Reverse the elements of the list\n");
    printf("10. EXIT   - End the program\n");
    printf("\n");
    printf("LIST : ");
    display(A);
    printf ("SIZE : %d\n",N);
    printf("\n");
    printf("Enter choice number: ");
    scanf ("%d",&choice);

      switch(choice)
    {
        case 1:
      {
        int num;
        printf("Enter the number to be appended: ");
        scanf("%d",&num);
        append(num);
        system("clear");
        break;
      }
        case 2:
      {
        extend();
        system("clear");
        break;
      }
        case 3:
      {
        int num, loc,k;
        printf("Enter the number to be inserted: ");
        scanf("%d",&num);
        printf("Enter the location where it needs to be inserted: ");
        scanf("%d",&loc);
        if (loc>0 && loc<=N)
        {
          insert(num,loc);
	  system("clear");
        }
        else
        {
          printf("The location entered doesn't exist!\n");
          getchar();getchar();
          system("clear");
        }
        break;
      }  
        case 4:
      {
        int num;
        printf("Enter the number to be removed: ");
        scanf("%d",&num);
        delete(num);
        system("clear");
        break;
      }
         case 5:
      {
        int pos,num;
        printf("Enter the position which has to be popped: ");
        scanf("%d",&pos);
        if (pos>0 && pos<=N)
        {
          num = pop(pos);
          printf("The popped number is %d\n",num);
          getchar();getchar();
          system("clear");
        }
        else
        {
          printf("The location entered doesn't exist!\n");
          getchar();getchar();
          system("clear");
        }
        break;
      }
        case 6:
      {
        int num;
        printf("Enter the number whose index needs to be returned: ");
        scanf("%d",&num);
        indexofnum(num);
        getchar();getchar();
        system("clear");
        break;
      }
        case 7:
      {
        int num;
        printf("Enter the number which needs to be counted: ");
        scanf("%d",&num);
        count(num);
        getchar();getchar();
        system("clear");
        break;
      }  
        case 8:
      {
        sort();
	system ("clear");
        break;
      }
        case 9:
      {
        reverse();
	system ("clear");
        break;
      }
        case 10:
      {
        exit(0);
        break;
      }
        default:
      {
        printf("Wrong Choice Entered!\n");
        break;
      }
    }
  }
  free (A);
} 
